package com.onesports.intelligent.k12.polarlight.service.coach;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.CoachCampusRelDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.CoachCampusRel;
import com.onesports.intelligent.k12.polarlight.mapper.organization.CoachCampusRelMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CoachCampusRelService extends ServiceImpl<CoachCampusRelMapper, CoachCampusRel> {

    public List<CoachCampusRel> findByCoachId(String coachId){
        return baseMapper.selectList(new LambdaQueryWrapper<CoachCampusRel>().eq(CoachCampusRel::getCoachId,coachId));
    }


    /**
     * function: 排序号如果存在 往后顺延一位
     *
     * @param coachCampusRel 教练社区(校区)关联表DTO
     * @author WangCX
     * @since 2022/8/3 15:47
     */
    public void addQueueNumber(List<CoachCampusRelDTO> coachCampusRel) {
        for (CoachCampusRelDTO dto : coachCampusRel) {
            Long aLong = baseMapper.countCampusRel(dto);
            if (aLong>0){
                LambdaUpdateWrapper<CoachCampusRel> lam = new LambdaUpdateWrapper<>();
                lam.eq(CoachCampusRel::getCampusId,dto.getCampusId())
                        .ge(CoachCampusRel::getQueueNumber,dto.getQueueNumber())
                        .setSql("queue_number = queue_number + 1");
                baseMapper.update(null,lam);
            }
        }
    }

    public Long countCampusRel(CoachCampusRelDTO relDTO) {
        return baseMapper.countCampusRel(relDTO);
    }

    public Long selectCoachNumber(String campusId){
        return baseMapper.selectCoachNumber(campusId);
    }
}
